package do

import (
	"fmt"
	"time"

	"gorm.io/gorm"
)

// 商场-负责人 关联表
type SupermarketDirector struct {
	ID            int `gorm:"primarykey"` // 自增id
	DirectorID    int `gorm:"primarykey"`
	SupermarketID int `gorm:"column:supermarket_id;NOT NULL;index"`

	// 负责人扣点,相当于提成,净利润里面要减掉，这个也是费用
	// 0.0x这种表示 x%
	DirectorPoints float32 `gorm:"column:director_points;default:0"`
	Note           string  `gorm:"column:note;default:'';comment:'备注'"`

	CreatedAt time.Time `gorm:"autoCreateTime;type:datetime(0)"`
	UpdatedAt time.Time `gorm:"autoUpdateTime;type:datetime(0)"`
}

func (s *SupermarketDirector) BeforeSave(tx *gorm.DB) (err error) {
	if s.DirectorID <= 0 {
		return fmt.Errorf("负责人id非法")
	}
	if s.SupermarketID <= 0 {
		return fmt.Errorf("超市id非法")
	}

	if s.DirectorPoints < 0 || s.DirectorPoints > 1 {
		return fmt.Errorf("负责人占比非法, 实际应该在[0, 100%%]之间, 现在为:%v", s.DirectorPoints)
	}

	return nil
}

func (*SupermarketDirector) TableName() string {
	return "supermarket_director"
}
